package com.fBlog.service.impl;


import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import com.fBlog.pojo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fBlog.dao.impl.AlphotoDaoI;
import com.fBlog.dao.impl.PhotoDaoI;
import com.fBlog.dao.impl.UserDaoI;


@Service
public class PhotoServiceImpl implements PhotoServiceI{

	@Autowired
	private AlphotoDaoI alphotoDao;
	
	@Autowired
	private PhotoDaoI photoDao;
	@Autowired
	private PhotoServiceI photoService;

	
	//通过用户id获取用户所有相册
	@Override
	public List<Alphoto> get(String id) {
		List<Alphoto> l = new ArrayList<Alphoto>();
		List<Talphoto> t = alphotoDao.find("from Talphoto t where t.user.id = '"+id+"' order by t.createTime");
		if(t!=null && t.size()>0){
			for(Talphoto a : t){
				Alphoto ap = new Alphoto();
				BeanUtils.copyProperties(a, ap);
				if(a.getUser()!=null){
					ap.setUserId(a.getUser().getId());
					ap.setUserName(a.getUser().getUserName());
					ap.setUserCreateTime(a.getUser().getCreateTime());
				}
				l.add(ap);
			}
		}
		return l;
	}


	@Override
	public void add(String name, String userId) {
		/*Talphoto ap = new Talphoto();
		ap.setId(id);
		ap.setCode("123");
		ap.setCreateTime(new Date());
		ap.setName(name);
		Tuser user = userDao.get("from Tuser t where t.id = '"+userId+"'");
		if(user!=null){
			ap.setUser(user);
		}
		
		alphotoDao.save(ap);*/
		String id = UUID.randomUUID().toString();
		String sql = "insert into talphoto (id,name,createTime,USERID) values ('"+id+"','"+name+"',now(),'"+userId+"')";
		alphotoDao.executeSql(sql);
	}


	@Override
	public List<Photo> getPhoto(String id) {
		List<Photo> l = new ArrayList<Photo>();
		List<Tphoto> t = photoDao.find("from Tphoto t where t.alphoto.id = '"+id+"' order by t.createTime");
		if(t!=null && t.size()>0){
			for(Tphoto a : t){
				Photo ap = new Photo();
				BeanUtils.copyProperties(a, ap);
				if(a.getAlphoto()!=null){
					ap.setAlphotoId(a.getAlphoto().getId());
					ap.setAlphotoName(a.getAlphoto().getName());
					ap.setAlphotoCreateTime(a.getAlphoto().getCreateTime());
				}
				l.add(ap);
			}
		}
		return l;
	}
	
	//通过相册id获取相册对象
	@Override
	public Alphoto getAlphoto(String id){
		Talphoto ap = alphotoDao.get("from Talphoto t where t.id = '"+id+"' order by t.createTime");
		if(ap!=null){
			Alphoto p = new Alphoto();
			BeanUtils.copyProperties(ap, p);
			if(ap.getUser()!=null){
				p.setUserId(ap.getUser().getId());
				p.setUserName(ap.getUser().getUserName());
				p.setUserCreateTime(ap.getUser().getCreateTime());
			}
			return p;
		}
		return null;
	}
	
	@Override
	public String addPhoto(String name, String alphotoId,String foldName) {
		/*Talphoto ap = new Talphoto();
		ap.setId(id);
		ap.setCode("123");
		ap.setCreateTime(new Date());
		ap.setName(name);
		Tuser user = userDao.get("from Tuser t where t.id = '"+userId+"'");
		if(user!=null){
			ap.setUser(user);
		}
		
		alphotoDao.save(ap);*/
		String id = UUID.randomUUID().toString();
		String url = "images\\\\albums\\\\"+foldName+"\\\\";
		//判断该相片是否存在在相应的相册文件夹中
		if(photoDao.get("from Tphoto t where t.url = '"+(url+name)+"'")==null){
			String sql = "insert into tphoto (id,url,name,createTime,ALPHOTOID) values ('"+id+"','"+(url+name)+"','"+name+"',now(),'"+alphotoId+"')";
			photoDao.executeSql(sql);
			return "0";
		}else{
			return "-1";
		}
	}

	@Override
	public String saveName(String alphotoId, String nowName,User user,String path,String foldName) {
		String code = "";
		//判断该用户是否已存在相同的相册名
		Talphoto tp = alphotoDao.get("from Talphoto t where t.user.id = '"+user.getId()+"' and t.id != '"+alphotoId+"' and t.name = '"+nowName+"'");
		if(tp!=null){
			code = "1";
		}else{
			//判断是否存在对应的相册文件夹，文件名为 用户名-相册名
			File file = new File(path+foldName);
			if(file.exists()){
				file.renameTo(new File(path+user.getUserName()+"-"+nowName));
			}
			String url = "images\\\\albums\\\\"+user.getUserName()+"-"+nowName+"\\\\";
			List<Tphoto> tphotos = photoDao.find("from Tphoto t where t.alphoto.id = '" + alphotoId + "'");
			if(tphotos!=null && tphotos.size()>0){
				for(Tphoto t : tphotos){
					photoDao.executeSql("update tphoto set url= '"+(url+t.getName())+"' where id = '"+t.getId()+"'");
				}
			}
			alphotoDao.executeSql("update talphoto set name= '"+nowName+"' where id = '"+alphotoId+"'");
			code = "0";
		}
		return code;
	}

	@Override
	public String deleteAlphoto(String alphotoId,User user,String path) {
		String code = "";

		//获取用户名和相册名作为该相册的文件夹。
		String userName = user.getUserName();
		String alphotoName = photoService.getAlphoto(alphotoId).getName();
		String foldName = userName + "-" + alphotoName;
		String foldPath = path+foldName;

		File file = new File(foldPath);
		if(file.exists()){
			deleteDir(file);
		}
		List<Tphoto> tphotos = photoDao.find("from Tphoto t where t.alphoto.id = '" + alphotoId + "'");
		if(tphotos!=null && tphotos.size()>0){
			for(Tphoto t : tphotos){
				photoDao.executeSql("delete from tphoto where id = '"+t.getId()+"'");
			}
		}
		alphotoDao.executeSql("delete from talphoto where id = '"+alphotoId+"'");
		code = "0";
		return code;
	}
	public boolean deleteDir(File dir) {
		if (dir.isDirectory()) {
			String[] children = dir.list();
			//递归删除目录中的子目录下
			for (int i=0; i<children.length; i++) {
				boolean success = deleteDir(new File(dir, children[i]));
				if (!success) {
					return false;
				}
			}
		}
		// 目录此时为空，可以删除
		return dir.delete();
	}

	@Override
	public String deletePhoto(String photoIds,String path) {
		String code = "0";
		String[] Ids = photoIds.split(",");
		for(String id : Ids){
			//首先删除该照片对应的图片
			//找到照片路径
			String filePath = path + photoDao.get(Tphoto.class,id).getUrl();
			File f = new File(filePath);
			f.delete();
			photoDao.executeSql("delete from tphoto where id = '"+id+"'");
		}
		return code;
	}
}
